package com.zq.factory;

import java.util.ResourceBundle;

/**
 * 工厂类
 */
public class FactoryBuilder {
    private static FactoryBuilder factoryBuilder;
    private FactoryBuilder(){

    }

    /**
     * 静态代码块实例化工厂
     */
    static{
        factoryBuilder = new FactoryBuilder();
    }

    /**
     * 提供静态的方法获取工厂对象
     * @return
     */
    public static FactoryBuilder getFactoryBuilder(){
        return factoryBuilder;
    }

    /**
     * 创建工厂方法，读取属性文件bean.properties通过反射创建实例对象
     * @param name
     * @return
     */
    public Object getBean(String name){
        //这里我通过ResourceBundle对象读取属性文件
        ResourceBundle bundle = ResourceBundle.getBundle("bean");
        String objectName = bundle.getString(name);
        Class clazz;
        Object obj=null;
        try {
            //动态加载类，得到实现类的类对象
            clazz = Class.forName(objectName);
            //通过反射创建实现类实例
            obj = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回实现类实例
        return obj;
    }
}
